/**
 * aui-popup.js
 * @author 流浪男
 * @todo more things to abstract, e.g. Loading css etc.
 * Licensed under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 */
(function (window, undefined) {
    "use strict";
    var auiToast = function () {
        // this.create();
    };
    var isShow = false;
    auiToast.prototype = {
        create: function (params, callback) {
            var self = this;
            var toastHtml = "";
            switch (params.type) {
                case "success":
                    var iconHtml =
                        '<i class="aui-iconfont aui-icon-correct"></i>';
                    break;
                case "fail":
                    var iconHtml =
                        '<i class="aui-iconfont aui-icon-close"></i>';
                    break;
                case "custom":
                    var iconHtml = params.html;
                    break;
                case "loading":
                    var iconHtml = '<div class="aui-toast-loading"></div>';
                    break;
            }

            var titleHtml = params.title
                ? '<div class="aui-toast-content">' + params.title + "</div>"
                : "";
            toastHtml =
                '<div class="aui-toast">' + iconHtml + titleHtml + "</div>";
            if (document.querySelector(".aui-toast")) return;
            document.body.insertAdjacentHTML("beforeend", toastHtml);
            var duration = params.duration ? params.duration : "2000";
            self.show();
            if (params.type == "loading") {
                if (callback) {
                    callback({
                        status: "success",
                    });
                }
            } else {
                setTimeout(function () {
                    self.hide();
                }, duration);
            }
        },
        show: function () {
            var self = this;
            document.querySelector(".aui-toast").style.display = "block";
            document.querySelector(".aui-toast").style.marginTop =
                "-" +
                Math.round(
                    document.querySelector(".aui-toast").offsetHeight / 2,
                ) +
                "px";
            if (document.querySelector(".aui-toast")) return;
        },
        hide: function () {
            var self = this;
            if (document.querySelector(".aui-toast")) {
                document
                    .querySelector(".aui-toast")
                    .parentNode.removeChild(
                        document.querySelector(".aui-toast"),
                    );
            }
        },
        remove: function () {
            if (document.querySelector(".aui-dialog"))
                document
                    .querySelector(".aui-dialog")
                    .parentNode.removeChild(
                        document.querySelector(".aui-dialog"),
                    );
            if (document.querySelector(".aui-mask")) {
                document
                    .querySelector(".aui-mask")
                    .classList.remove("aui-mask-out");
            }
            return true;
        },
        success: function (params, callback) {
            var self = this;
            params.type = "success";
            return self.create(params, callback);
        },
        fail: function (params, callback) {
            var self = this;
            params.type = "fail";
            return self.create(params, callback);
        },
        custom: function (params, callback) {
            var self = this;
            params.type = "custom";
            return self.create(params, callback);
        },
        loading: function (params, callback) {
            var self = this;
            params.type = "loading";
            return self.create(params, callback);
        },
    };
    window.auiToast = auiToast;
})(window);
